/*
 * @ProjectName: 综合安防
 * @Copyright:   2018 HangZhou Hikvision System Technology Co., Ltd. All Right Reserved.
 * @address:     http://www.hikvision.com
 * @date:        2018年01月29日 14:21
 * @description: 本内容仅限于杭州海康威视系统技术公有限司内部使用，禁止转发.
 */
package com.capsule.chapter.time;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 *
 * </p>
 *
 * @author xiachaoyang
 * @version V1.0
 * @date 2018年07月06日 17:36
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2018年07月06日
 * @modify reason: {方法名}:{原因}
 * ...
 */
public class DateTimeUtils {

    public static final String DATA_FORMAT_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";

    //2018-05-18T20:20:53.420+08:00
    public static final String DATA_FORMAT_yyyy_MM_dd_T_HH_mm_ss_SSS_ADD_XXX = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
    //T20:20:53.420+08:00
    public static final String DATA_FORMAT_T_HH_mm_ss_SSS_ADD_XX = "'T'HH:mm:ss.SSSXXX";
    //T20:20+08:00
    public static final String DATA_FORMAT_T_HH_mm_ADD_XX = "'T'HH:mmXXX";
    //T20:20:00.000+08:00
    public static final String DATA_FORMAT_T_HH_mm_s0_ADD_XX = "'T'HH:mm':00.000'XXX";

    public static final String DATA_FORMAT_yyyy_MM_dd = "yyyy-MM-dd";

    public static final String DATE_FORMAT_yyyy_MM_dd_T_HH_mm_ss_SSSZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

    public static final String DATE_FORMAT_yyyy_MM_dd_HH_mm_ss_SSS = "yyyy-MM-dd HH:mm:ss.SSS";

    /**
     * 获取相邻日期
     *
     * @param diff 正数表示未来天，负数表示过去天，比如：-1表示昨天日期
     * @author xiachaoyang
     * @date 2018/7/6 17:30
     * @modify By:
     * @modify reason:
     */
    public static String getAdjacentDay(Date date, int diff) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        cal.add(Calendar.DATE, diff);
        Date time = cal.getTime();
        return new SimpleDateFormat(DATA_FORMAT_yyyy_MM_dd_HH_mm_ss).format(time);
    }

    /**
     * 时间格式化
     *
     * @param date
     * @param fmt
     * @return
     */
    public static String dateFmtToString(Date date, String fmt) {
        return new SimpleDateFormat(fmt).format(date);
    }


    @Test
    public void dateFunctions() {
        System.out.println(new Date().getHours());
    }

    /**
     * 换化为带时区的时间格式
     * 2018-08-21T11:06:19.846+08:00
     * T11:06:19.847+08:00
     * T11:06+08:00
     * T11:06:00.000+08:00
     * SimpleDateFormat只能处理3位小数
     *
     * @throws ParseException
     */
    @Test
    public void timeWithZone() {
        String t1 = new SimpleDateFormat(DATA_FORMAT_yyyy_MM_dd_T_HH_mm_ss_SSS_ADD_XXX).format(new Date());
        String t2 = new SimpleDateFormat(DATA_FORMAT_T_HH_mm_ss_SSS_ADD_XX).format(new Date());
        String t3 = new SimpleDateFormat(DATA_FORMAT_T_HH_mm_ADD_XX).format(new Date());
        String t4 = new SimpleDateFormat(DATA_FORMAT_T_HH_mm_s0_ADD_XX).format(new Date());
        String t5 = new SimpleDateFormat(DATA_FORMAT_yyyy_MM_dd).format(new Date());
        System.out.println(t1);
        System.out.println(t2);
        System.out.println(t3);
        System.out.println(t4);
        System.out.println(t5);
    }

    /**
     * 解析
     * 12:20 - > T12:20:00.000+08:00
     * 为带时区信息的
     *
     * @param timeStr
     * @return
     */
    public static String getHMTimeWithZone(String timeStr) {
        if (StringUtils.isBlank(timeStr) || timeStr.length() != 5) {
            return "";
        }
        String zoneStr = new SimpleDateFormat(DATA_FORMAT_T_HH_mm_s0_ADD_XX).format(new Date());
        return new StringBuilder(zoneStr.substring(0, 1)).append(timeStr).append(zoneStr.substring(6)).toString();
    }

    /**
     * 将带时区信息的时间转化为本地时间
     * 2016-06-08T16:58:23.000+07:00 2016-06-08 17:58:23.000
     * @param dateTime
     * @return
     */
    public static String formatISO8601ToLocalDateTime(String dateTime) {
        DateTimeFormatter isoformat = DateTimeFormat.forPattern(DATE_FORMAT_yyyy_MM_dd_T_HH_mm_ss_SSSZ);
        DateTime dt= isoformat.parseDateTime(dateTime);
        DateTimeFormatter format = DateTimeFormat.forPattern(DATE_FORMAT_yyyy_MM_dd_HH_mm_ss_SSS);
        return dt.toString(format);
    }

    /**
     * 将带时区信息的时间转化为本地时间
     * 2016-06-08T16:58:23.000+07:00
     * output 2016-06-08 17:58:23.000
     */
    public static String formatISO8601ToLocalDateTime2(String timeStr) {
        try{
        SimpleDateFormat sdfSource = new SimpleDateFormat(DATA_FORMAT_yyyy_MM_dd_T_HH_mm_ss_SSS_ADD_XXX);//原始的日期格式
        SimpleDateFormat sdfDest = new SimpleDateFormat(DATE_FORMAT_yyyy_MM_dd_HH_mm_ss_SSS);//目标日期格式
         return sdfDest.format(sdfSource.parse(timeStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
       //System.out.println(formatISO8601Date("2016-06-08T16:58:23.000+08:00", null));
        System.out.println(formatISO8601ToLocalDateTime2("2016-06-08T16:58:23.000+07:00"));
        //System.out.println(getHMTimeWithZone("12:56"));
        //System.out.println(getAdjacentDay(new Date(),-1));
        //System.out.println(dateFmtToString(new Date(),DATA_FORMAT_yyyy_MM_dd));
    }
}
